const mongoConnected = require("../lib/mongoose_connect");
const mongoModels = require("../lib/mongoose_schemas");
const fs = require("fs-extra");
const path = require("path");
const Data0805 = require("../test/0805-雨林牛逼玩家.json");
const Data080901 = require("../test/0809_1_高手玩家及其雨林4排match.json");

//把一个玩家在多场雨林地图里的路径拿出来看
let testName = "groppoRJ"

mongoConnected.then(async $ => {
  let record1 = Data080901.filter(e=>e.player.name==testName)[0]
  debugger
  console.log(record1.player.name);
  let store = [];
  for (let mi of record1.savage_squad_matchids) {
    let matchinfo = await mongoModels.Match.findOne({
      match_id: mi
    });
    let startTime = matchinfo.start_time;
    let evParachuteLanding = await mongoModels.Event.findOne({
      match_id: mi,
      _T: "LogParachuteLanding",
      "character.accountId": record1.player.accountId
    });
    let landingTime = evParachuteLanding._D;
    // debugger
    let evs = await mongoModels.Event.find({
      match_id: mi,
      _D: {
        $gte: landingTime
      },
      $or: [
        { "killer.accountId": record1.player.accountId },
        { "attacker.accountId": record1.player.accountId },
        { "victim.accountId": record1.player.accountId },
        { "assistant.accountId": record1.player.accountId },
        { "character.accountId": record1.player.accountId },
      ]
    }).sort({
      _D: 1
    });
    // debugger
    let locations = evs.map(ev => {
      if (ev._doc.character) {
        return ev._doc.character.location
      }
      if (ev._doc.killer && ev._doc.killer.accountId == record1.player.accountId) {
        return ev._doc.killer.location
      }
      if (ev._doc.attacker && ev._doc.attacker.accountId == record1.player.accountId) {
        return ev._doc.attacker.location
      }
      if (ev._doc.victim && ev._doc.victim.accountId == record1.player.accountId) {
        return ev._doc.victim.location
      }
      if (ev._doc.assistant && ev._doc.assistant.accountId == record1.player.accountId) {
        return ev._doc.assistant.location
      }
    });
    for (let i = locations.length - 1; i >= 1; i--) {
      if (JSON.stringify(locations[i]) == JSON.stringify(locations[i - 1])) {
        locations[i].x = 0;
        locations[i].y = 0;
        locations[i].z = 0;
        // console.log("重复")
      }
    }
    // debugger
    locations = locations.filter(e => !(e.x == 0 && e.y == 0 && e.z == 0));
    store.push({
      match_id: mi,
      locations: locations
    })
    // debugger
  }
  fs.writeFile(path.join(__dirname, `../test/0809_6_${testName}在雨林四排的多场地面移动路线.json`), JSON.stringify(store), (err) => {
    debugger
    process.exit()
  })
  debugger

})

